package com.yz.controller.blog;

import com.yz.pojo.Blog;
import com.yz.pojo.BlogCategory;
import com.yz.pojo.Comment;
import com.yz.pojo.Tag;
import com.yz.pojo.dto.BlogDto;
import com.yz.service.BlogService;
import com.yz.service.CategoryService;
import com.yz.service.CommentService;
import com.yz.service.TagService;
import com.yz.util.Result;
import com.yz.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@SuppressWarnings("all")
public class MyBlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CategoryService categoryService;

    // 首页
    @RequestMapping({"/", "index.html", "index"})
    public String showBlog(Model model, @RequestParam(required = false, defaultValue = "1") Integer page) {
        // 分页起始位置
        Integer start = 0;
        // 每页的个数
        Integer count = 5;
        start = (page - 1) * count;
        // 查询所有的博客
        List<BlogDto> allBlog = blogService.pagingQuery(start, count);
        // 把 tags 里面的, 换成 / ，比较好看点
        for (BlogDto blogDto : allBlog) {
            blogDto.setBlogTags(blogDto.getBlogTags().replace(","," / "));
        }
        model.addAttribute("allBlog", allBlog);
        // 已经发布的博客总数
        Integer blogNums = blogService.countBlog(1);
        // 总页数
        Integer pages = blogNums % count == 0 ? (blogNums / count) : (blogNums / count) + 1;
        model.addAttribute("pages", pages);
        // 当前页数
        model.addAttribute("page", page);
        // 查询所有的标签
        List<Tag> tags = tagService.queryAllTags();
        model.addAttribute("tags", tags);
        // 查询所有分类
        List<BlogCategory> blogCategories = categoryService.queryAllCategory();
        model.addAttribute("blogCategories", blogCategories);
        // 查询浏览最高的四篇博客
        List<BlogDto> blogDtos = blogService.queryTopViewsBlogs();
        model.addAttribute("blogDtos", blogDtos);
        // 查询最新发布的四篇博客
        List<BlogDto> recentBlogs = blogService.queryRentBlogs();
        model.addAttribute("recentBlogs", recentBlogs);
        return "blog/index";
    }

    // 博客详情页
    @GetMapping("/details/{blogId}")
    @Transactional
    public String test(Model model, @PathVariable("blogId") Integer blogId) {
        // 判断是否存在这篇博客
        if (blogService.queryBlogById(blogId) == null) return "error/error_404";
        // 查询博客评论数
        int count = commentService.count(blogId);
        model.addAttribute("count", count);
        // 查询博客的所有评论
        List<Comment> comments = commentService.queryComments(blogId);
        model.addAttribute("comments", comments);
        // 博客浏览数 + 1
        if (blogService.viewsIncr(blogId)) {
            // 根据博客id查询博客
            Blog blog = blogService.queryBlogById(blogId);
            model.addAttribute("blog", blog);
        }
        return "blog/details";
    }

    // 添加评论
    @RequestMapping("/addComment")
    @ResponseBody
    @Transactional
    public Result addComment(@RequestParam("blogId") Integer blogId, @RequestParam("blogName") String blogName, @RequestParam("commentator") String commentator,
                             @RequestParam("email") String email, @RequestParam("commentBody") String commentBody) {
        Comment comment = new Comment(blogId, blogName, commentator, email, commentBody);
        if (commentService.addComment(comment) == 1) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("提交评论失败");
    }

    // 通过关键字查询博客
    @GetMapping("/search/{keyword}")
    public String search(Model model, @PathVariable(required = false) String keyword) {
        List<BlogDto> queryBlogByTitleOrCategory = blogService.queryBlogByTitleOrCategory(keyword);
        model.addAttribute("allBlog", queryBlogByTitleOrCategory);
        // 查询所有的标签
        List<Tag> tags = tagService.queryAllTags();
        model.addAttribute("tags", tags);
        // 查询所有类别
        List<BlogCategory> blogCategories = categoryService.queryAllCategory();
        model.addAttribute("blogCategories",blogCategories);
        // 查询浏览最高的四篇博客
        List<BlogDto> blogDtos = blogService.queryTopViewsBlogs();
        model.addAttribute("blogDtos", blogDtos);
        // 查询最新发布的四篇博客
        List<BlogDto> recentBlogs = blogService.queryRentBlogs();
        model.addAttribute("recentBlogs", recentBlogs);
        return "blog/search";
    }

    // 通过标签查询博客
    @GetMapping("/queryForTagId/{tagId}")
    public String queryForTagId(Model model, @PathVariable("tagId") Integer tagId) {
        List<BlogDto> queryForTagId = blogService.queryForTagId(tagId);
        if (queryForTagId == null) return "error/error_404";
        model.addAttribute("allBlog", queryForTagId);
        // 查询所有的标签
        List<Tag> tags = tagService.queryAllTags();
        model.addAttribute("tags", tags);
        // 查询所有类别
        List<BlogCategory> blogCategories = categoryService.queryAllCategory();
        model.addAttribute("blogCategories",blogCategories);
        // 查询浏览最高的四篇博客
        List<BlogDto> blogDtos = blogService.queryTopViewsBlogs();
        model.addAttribute("blogDtos", blogDtos);
        // 查询最新发布的四篇博客
        List<BlogDto> recentBlogs = blogService.queryRentBlogs();
        model.addAttribute("recentBlogs", recentBlogs);
        return "blog/search";
    }

    // 通过分类查询博客
    @GetMapping("/queryForCategoryId/{categoryId}")
    public String queryForCategoryId(Model model, @PathVariable("categoryId") Integer categoryId, @RequestParam(required = false, defaultValue = "1") Integer page) {
        // 分页起始位置
        Integer start = 0;
        // 每页的个数
        Integer count = 5;
        start = (page - 1) * count;
        // 已发布的博客总数
        Integer blogNums = blogService.countForCategoryId(categoryId);
        // 根据类别 Id 查询博客
        List<BlogDto> allBlog = blogService.queryForCategoryId(start,count,categoryId);
        model.addAttribute("allBlog",allBlog);
        // 总页数
        Integer pages = blogNums % count == 0 ? (blogNums / count) : (blogNums / count) + 1;
        model.addAttribute("pages", pages);
        // 当前页数
        model.addAttribute("page", page);
        // 查询所有的标签
        List<Tag> tags = tagService.queryAllTags();
        model.addAttribute("tags", tags);
        // 查询所有类别
        List<BlogCategory> blogCategories = categoryService.queryAllCategory();
        model.addAttribute("blogCategories",blogCategories);
        // 查询浏览最高的四篇博客
        List<BlogDto> blogDtos = blogService.queryTopViewsBlogs();
        model.addAttribute("blogDtos", blogDtos);
        // 查询最新发布的四篇博客
        List<BlogDto> recentBlogs = blogService.queryRentBlogs();
        model.addAttribute("recentBlogs", recentBlogs);
        return "blog/categoryList";
    }

    // 文件传输
    @RequestMapping({"/ftpFile", "ftpFile.html", "ftpFile"})
    public String ftpFile(Model model) {
        return "blog/ftpFile";
    }
}
